(function($) {
	$.fn.extend({
		printFacebookShareButton: function(options) {
			var defaults = {
				shareText: "+Share",
				location: location.hostname == "localhost" ? '127.0.0.1' : location.href
			};
			var options =  $.extend(defaults, options);

			var hostName = options.location;
			//var fql = 'SELECT url, share_count, like_count, comment_count, total_count FROM link_stat WHERE url="'+hostName+'"';
			//var url = 'https://api.facebook.com/method/fql.query?format=json&query=' + encodeURI(fql)+"&callback=?";
			var url = '//graph.facebook.com/?id=' + document.location.href + "&callback=?";
			var shareCount= 0;

			jQuery.getJSON( url ,function(data){
			//console.log(data);
				try{
				if(data.total_count)
					shareCount = data.total_count.shares;
				else if(data.shares)
					shareCount = data.shares;
				else
					shareCount = 0;
				//shareCount = 1231234568521;
				var million= Math.pow(10,6), billion = Math.pow(10,9);

				if( shareCount <10000) {;}
				else if( shareCount >= 10000 && shareCount < million) shareCount= Math.floor(shareCount/1000) + 'K+';
				else if( shareCount >= million && shareCount < billion) shareCount= Math.floor(shareCount/million) + 'M+';
				else if( shareCount >= billion && (shareCount/billion < 1000) ) shareCount = Math.floor(shareCount / billion) + 'B+';
				else shareCount = '999B+';

				$('#fbShareCountDivID').html(shareCount);
				}catch(e) {console.log(e);}

			});

			// this function is written to reduce the pain to mask quotes in the window.open func
			var behaviourPostShareToFacebook = function ()
			{
			window.open('//www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href), 'facebook-share-dialog', 'width=626,height=436');
			return false;
			}

			//var divID= "postShareCount";
			var html = "";

			// render page html
			html +='<div style="background-color: #423124;width:60px;height:40px;padding:1px;color:#D0BFBF;text-align:center" >' ;
			html += '<a style= "font-weight:bold;font-size:18px;color:#FFFFFF;" href="" id="fbShareCountDivID">'+shareCount+'</a>';
			html += '<br />Times</div>';

			html += '<div style="background:#3333CC;width:60px;padding:1px;text-align:center;">';
			html += '<a id="facebooksharecount" href="" style="font-size:14px;color:#FFFFFF" title="Share on Facebook" ';
			html += '>'+options.shareText+'</a></div>';

			document.write(html);
			$('#facebooksharecount').click(
			function (){
			window.open('//www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href), 'facebook-share-dialog', 'width=626,height=436');
			return false;
			}
			); //= behaviourPostShareToFacebook;
		}
	});
})(jQuery);